<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.ld.xuanKe.entity.Course" %>
<%@ page import="java.util.List" %>
<%@ page import="com.ld.xuanKe.entity.User" %>
<%@ page import="org.springframework.context.ApplicationContext" %>
<%@ page import="static org.apache.struts2.ServletActionContext.getServletContext" %>
<%@ page import="org.springframework.web.context.WebApplicationContext" %>
<%@ page import="com.ld.xuanKe.service.SelectdService" %>
<%@ page import="com.ld.xuanKe.service.impl.SelectdServiceImpl" %>
<%@ page import="com.ld.xuanKe.entity.Selectd" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="com.ld.xuanKe.entity.MySelect" %><%--
  Created by IntelliJ IDEA.
  User: 刘邓
  Date: 2023-06-10
  Time: 16:13
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
    <title>我的课程</title>
    <link rel="stylesheet" type="text/css" href="/common.css"/>
</head>
<body>

<%
    ApplicationContext applicationContext = (ApplicationContext) getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    SelectdService selectdService = (SelectdServiceImpl) applicationContext.getBean(SelectdService.class);
    String name = request.getParameter("name");
    User user = (User) session.getAttribute("user");
    List<MySelect> selectds = new ArrayList<>();
    if (name == null) {
        selectds = selectdService.showMyCourse(user.getId());
    } else {
        selectds = selectdService.showMyCourseLikeName(user.getId(), name);
    }
    String homePath;
    if (user.getStatus() == 0) {
        homePath = "user";
    } else {
        homePath = "admin";
    }
%>
<a href="/home/<%=homePath%>/index.jsp"
   style="display: inline-block;height: 30px;margin:10px 30px;text-align:center;line-height:30px;background-color:#DDD;text-decoration: none;">返回个人管理页面</a>
<h3>当前学分为:<%=selectdService.getMyCredit(user.getId())%></h3>
<form id='like' action='/home/user/myCourse.jsp' method='get'><input name='name'>&nbsp&nbsp<input type='submit' value='按课程名模糊查询'>
</form>
<div class='navigate'><a href='/selectd/downLoad'>导出为excel</a></div>
<c:if test="<%=selectds.isEmpty()%>">
    <h1 style='width: 80%;height:400px;margin: 0 auto;text-align:center;line-height:400px'>暂无更多信息!</h1>
</c:if>
<c:if test="<%=!selectds.isEmpty()%>">
    <table id='all-request' class='container'>

        <thead>
        <tr>
            <th>选课id</th>
            <th>课程名字</th>
            <th>授课老师</th>
            <th>选课时间</th>
            <th>课程说明</th>
            <th>学分</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items="<%=selectds%>" var="selectd">
            <tr>
                <td>${selectd.getId() }</td>
                <td>${selectd.getCourseName()}</td>
                <td>${selectd.getTeacherName()}</td>
                <td>${selectd.getTime()}</td>
                <td>${selectd.getBz()}</td>
                <td>${selectd.getXf()}</td>
                <td class='accept toDelete'><a class='deleted' href="/selectd/delete?id=${selectd.getId()}">退课</a></td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</c:if>

</body>
<script>
    let deleted = document.getElementsByClassName("deleted");
    for (let i = 0; i < deleted.length; i++) {
        deleted[i].addEventListener("click", (e) => {
            let b = confirm("你确定要选择该课程吗?");
            if (b == false) {
                e.preventDefault();
            }
        })
    }
</script>
</html>
